﻿using DocumentFormat.OpenXml.Math;
using log4net.Util;
using SiliconSelect.Base;
using SiliconSelect.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace SiliconSelect.Views.UserOperatorDialog
{
    /// <summary>
    /// InputNextCrystalWindow.xaml 的交互逻辑
    /// </summary>
    public partial class InputNextCrystalWindow : Window
    {
        InputNextCrystalViewModel inputNextViewModel = new InputNextCrystalViewModel();
        public InputNextCrystalWindow()
        {
            InitializeComponent();
            DataContext = inputNextViewModel;
            inputNextViewModel.LotNumber = GlobalValues.ProductionLot;
            inputNextViewModel.PositionNumber = GlobalValues.PositionNumber;
            inputNextViewModel.FlowerNumber = GlobalValues.FlowerNumber;
            inputNextViewModel.CrystalNumber = GlobalValues.CrystalNumberNext;
            inputNextViewModel.TheoryCount = 4000;
        }

        private Point _pressedPositon;
        bool _isDragMove = false;

        // 记录鼠标按下位置
        private void Window_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if ((sender as FrameworkElement)!.Name == "Button")
            {
                e.Handled = false;
            }
            else
            {
                _pressedPositon = e.GetPosition(this);

            }
        }

        // 鼠标移动触发拖动
        private void Window_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            if (Mouse.LeftButton == MouseButtonState.Pressed && _pressedPositon != e.GetPosition(this) && (sender as FrameworkElement)!.Name != "Button")
            {
                _isDragMove = true;
                DragMove();
            }
        }

        // 鼠标弹起屏蔽消息
        private void Window_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (_isDragMove && (sender as FrameworkElement)!.Name != "Button")
            {
                _isDragMove = false;
                e.Handled = true; // 表示事件已经处理,其他不再处理这个事件
            }
        }

        private void BtnOkClick(object sender, RoutedEventArgs e)
        {
            if (inputNextViewModel.TheoryCount == 0)
            {
                MessageBox.Show("输入错误,理论数量不能是0", "晶棒信息错误提示框!", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            if (string.IsNullOrEmpty(inputNextViewModel.CrystalNumber))
            {
                MessageBox.Show("晶棒编号不能为空", "晶棒信息错误提示框!", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            Regex regStart = new Regex("^[a-zA-Z]");
            if (!regStart.IsMatch(inputNextViewModel.CrystalNumber))
            {
                MessageBox.Show("晶棒编号必须以字母开头!", "晶棒信息错误提示框!", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }


            // 保存下一单数据
            GlobalValues.ProductionLotNext = inputNextViewModel.LotNumber;
            GlobalValues.PostionNumberNext = inputNextViewModel.PositionNumber;
            GlobalValues.FlowerNumberNext = inputNextViewModel.FlowerNumber;
            GlobalValues.TheoryNumberNext = inputNextViewModel.TheoryCount;
            GlobalValues.CrystalNumberNext = inputNextViewModel.CrystalNumber;
            Close();
        }

        private void BtnCancelClick(object sender, RoutedEventArgs e)
        {
            // 取消保存
            Close();
        }

        private async void FocusInput_GetFocus(object sender, RoutedEventArgs e)
        {
            await Task.Delay(10);
            focusInput.CaretIndex = inputNextViewModel.CrystalNumber!.Length;
        }
    }
}
